package org.lex.tinyscript;

import java.io.File;
import java.io.IOException;
import java.io.Writer;

public class TinyScriptBuilder {
	private StringBuilder buf = null;
	private String nl = null;

	public TinyScriptBuilder() {
		buf = new StringBuilder();
		nl = System.getProperty("line.separator");
		if (null == nl)
			nl = "\r\n";
	}

	public void store(Writer out) throws IOException {
		out.write(buf.toString());
		out.flush();
	}

	public TinyScriptBuilder delete(File file) {
		buf.append("delete ");
		this.appendFile(file);
		buf.append(nl);
		return this;
	}

	public TinyScriptBuilder rename(File from, File to) {
		return this.move(from, to);
	}

	public TinyScriptBuilder move(File from, File to) {
		buf.append("move ");
		this.appendFile(from);
		buf.append(' ');
		this.appendFile(to);
		buf.append(nl);
		return this;
	}

	public TinyScriptBuilder java(File propsFile) {
		buf.append("java ");
		this.appendFile(propsFile);
		buf.append(nl);
		return this;
	}

	public String toString() {
		return buf.toString();
	}

	private void appendFile(File file) {
		buf.append('<').append(file.getAbsolutePath()).append('>');
	}
}
